// pages/lesson/index.js
import {
  request
} from "../../request/index.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [{
        id: 0,
        name: "可选课程",
        isActitv: true
      },
      {
        id: 1,
        name: "已选课程",
        isActitv: false
      },
    ],
    course1: [],
    course2: []
  },
  handleItemChange(e) {
    const {
      index
    } = e.detail;
    let {
      tabs
    } = this.data;
    tabs.forEach((v, i) => i === index ? v.isActitv = true : v.isActitv = false);
    this.setData({
      tabs
    })
  },
  bindcourseChange(e) {
    const that = this;
    wx.getStorage({
      key: 'id',
      success(res) {
        request({
          url: "http://localhost:3088/scure/",
          method: "POST",
          data: {
            uid: res.data,
            cid: e.detail.data.id,
          }
        }).then(res1 => {
          wx.showToast({
            title: res1.data.message,
          })
          if (res1.statusCode === 200) {
            setTimeout(() => {
              let r = that.data.course1;
              let t = that.data.course2;
              for (let i = 0; i < r.length; i++) {
                if (JSON.stringify(e.detail.data) === JSON.stringify(that.data.course1[i])) {
                  r[i].select=true;
                  r[i].statusNum=r[i].statusNum+1;
                  t.push(r[i]);
                  r.splice(i, 1);
                  that.setData({
                    course1: r,
                  })
                }
                that.setData({
                  course2: t,
                })
              }
            },1500);
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this;
    wx.getStorage({
      key: 'id',
      success(res) {
        request({
          url: 'http://localhost:3088/scure/' + res.data
        }).then(result => {
          let a = [];
          let b = [];
          result.data.forEach(e => {
            if (e.select) {
              a.push(e);
            } else {
              b.push(e);
            }
          })
          that.setData({
            course1: b
          })
          that.setData({
            course2: a
          })
          wx.setStorage({
            data: a,
            key: 'course',
          })
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})